﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrameWork.Components;
using System.Collections;
using FrameWork.WebControls;

namespace FrameWork.web.Module.IntegralMS.IntegralMS.ClassManager
{
    public partial class Edit : System.Web.UI.Page
    {
        int ClassID = (int)Common.sink("ClassID", MethodType.Get, 255, 1, DataType.Int);
        string CMD = (string)Common.sink("CMD", MethodType.Get, 255, 0, DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            BindButton();
            if (!Page.IsPostBack)
            {
                BingdListBox();
                OnStart();
            }
        }
        private void BingdListBox()
        {
            QueryParam qp = new QueryParam();
            int RecordCount;
            ArrayList list = BusinessFacade.sys_UserList(qp, out RecordCount);
            sys_UserTable user = new sys_UserTable();
            user.UserID = 0;
            user.U_CName = "无";
            list.Insert(0, user);
            lboxUserList.DataSource = list;
            lboxUserList.DataTextField = "U_CName";
            lboxUserList.DataValueField = "UserID";
            lboxUserList.DataBind();

        }
        private void OnStart()
        {
            QueryParam qp1 = new QueryParam();
            int RecordCount1 = 0;
            qp1.Where = "WHERE R_ClassID="+ClassID;
            ArrayList list = BusinessFacade.stu_ClassTeacherList(qp1, out RecordCount1);
            string userIDStr = "";
            foreach (stu_ClassTeacherRelationTable item in list)
            {
                userIDStr += item.R_TeacherID+",";
            }
            foreach (ListItem item in lboxUserList.Items)
            {
                if (userIDStr != "" && userIDStr.Contains(item.Value))
                {
                    item.Selected = true;
                }
            }
            stu_ClassInfoTable gt = BusinessFacade.stu_ClassInfoDisp(ClassID);
            this.G_CName.Text = gt.C_CName;
            if (gt.C_ParentID == 0)
                G_ParentID_Txt.Text = "部门列表";
            else
                G_ParentID_Txt.Text = BusinessFacade.stu_ClassInfoDisp(gt.C_ParentID).C_CName;
            CatListTitle.Text = string.Format("<a href='GroupList.aspx'>部门列表</a>{0}", BusinessFacade.GetClassInfoTitle(ClassID));
            //判断是否删除
            if (CMD == "Delete")
            {
                FrameWorkPermission.CheckPermissionVoid(PopedomType.Delete);

                //删除子分类
                DeleteCat(ClassID);
                //删除当前分类
                BusinessFacade.Update_Table_Fileds("stu_ClassInfo", "C_Delete=1", string.Format("ClassID={0}", ClassID));
                if (gt.C_ParentID != 0)
                {
                    //更新父类子类数
                    BusinessFacade.Update_Table_Fileds("stu_ClassInfo", "C_ChildCount=C_ChildCount-1", string.Format("ClassID={0}", gt.C_ParentID));
                    //更新父类子类排序  
                    int RecordCount = 0;
                    QueryParam qp = new QueryParam();
                    qp.Where = string.Format("Where C_ParentID={0} and C_Delete=0", gt.C_ParentID);
                    qp.Orderfld = "C_Level,C_ShowOrder";
                    qp.OrderType = 0;
                    ArrayList lst = BusinessFacade.stu_ClassInfoList(qp, out RecordCount);
                    RecordCount = 1;
                    foreach (stu_ClassInfoTable var in lst)
                    {
                        BusinessFacade.Update_Table_Fileds("stu_ClassInfo", string.Format("C_ShowOrder={0}", RecordCount), string.Format("ClassID={0}", var.ClassID));
                        RecordCount++;
                    }

                }
                EventMessage.MessageBox(1, "操作成功", string.Format("删除部门({0})成功!", gt.C_CName), Icon_Type.OK, Common.GetHomeBaseUrl("GroupList.aspx"), Common.BuildJs);

            }
        }

        private void BindButton()
        {
            HeadMenuButtonItem bi1 = new HeadMenuButtonItem();
            bi1.ButtonPopedom = PopedomType.Delete;
            bi1.ButtonUrl = string.Format("DelData('?CMD=Delete&ClassID={0}')", ClassID);
            bi1.ButtonUrlType = UrlType.JavaScript;
            HeadMenuWebControls1.ButtonList.Add(bi1);

            HeadMenuButtonItem bi0 = new HeadMenuButtonItem();
            bi0.ButtonIcon = "back.gif";
            bi0.ButtonName = "返回";
            bi0.ButtonPopedom = PopedomType.List;
            bi0.ButtonUrl = string.Format("GroupList.aspx?ClassID={0}", ClassID);
            HeadMenuWebControls1.ButtonList.Add(bi0);
        }
        private void DeleteCat(int ClassID)
        {
            //删除子分类和删除当前分类
            int RecordCount = 0;
            QueryParam qp = new QueryParam();
            qp.Where = string.Format("Where C_ParentID={0} and C_Delete = 0 ", ClassID);
            ArrayList lst = BusinessFacade.stu_ClassInfoList(qp, out RecordCount);
            foreach (stu_ClassInfoTable var in lst)
            {
                BusinessFacade.Update_Table_Fileds("stu_ClassInfo", "C_Delete=1", string.Format("ClassID={0}", var.ClassID));
                DeleteCat(var.ClassID);
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            FrameWorkPermission.CheckPermissionVoid(PopedomType.Edit);
            stu_ClassInfoTable gt = BusinessFacade.stu_ClassInfoDisp(ClassID);
            gt.C_CName = (string)Common.sink(G_CName.UniqueID, MethodType.Post, 50, 1, DataType.Str);
            gt.C_UserIDStr = "";
            stu_ClassTeacherRelationTable s1 = new stu_ClassTeacherRelationTable {
            
                R_ClassID=ClassID,
                R_TeacherID=0,
                DB_Option_Action_="Delete"
            };
            BusinessFacade.stu_ClassTeacherInsertUpdate(s1);
            foreach (ListItem item in lboxUserList.Items)
            {
                if (item.Selected)
                {
                    stu_ClassTeacherRelationTable s = new stu_ClassTeacherRelationTable { 
                        R_ClassID=ClassID,
                        R_TeacherID=Convert.ToInt32(item.Value),
                        DB_Option_Action_="Insert"
                    };
                    BusinessFacade.stu_ClassTeacherInsertUpdate(s);
                }
            }
            gt.DB_Option_Action_ = "Update";
            BusinessFacade.stu_ClassInsertUpdate(gt);
            EventMessage.MessageBox(1, "操作成功", string.Format("修改部门ID({0})成功!", gt.ClassID), Icon_Type.OK, Common.GetHomeBaseUrl(string.Format("GroupList.aspx?ClassID={0}", ClassID)), Common.BuildJs);
        }
    }
}